﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;


namespace GenetecHackathonCod3
{
    public partial class MainPage : PhoneApplicationPage
    {

        public static List<Report> reports = new List<Report>();

        public static int selected;

        //XmlSerializer serializer = new XmlSerializer(typeof(List<Report>));

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            load();
            search.Text = "";
            populateList();
        }

        private void NewReport_Click(object sender, EventArgs e)
        {
          NavigationService.Navigate(new Uri("/NewReport.xaml", UriKind.Relative));
        }

        private void ListPanel_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (ListPanel.SelectedIndex != -1)
            {

                selected = ListPanel.SelectedIndex;

                NavigationService.Navigate(new Uri("/EditReport.xaml", UriKind.Relative));

                ListPanel.SelectedIndex = -1;
            }
        }

        public static List<Report> getReports()
        {
            return reports;
        }

        private void populateList()
        {

            foreach (Report report in reports)
            {
                TextBlock item = new TextBlock();
                item.Text = "" + report.getTitle() + "\n" + report.getDateString();
                ListPanel.Items.Add(item);
            }
            
        }

        public static void save()
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream file = store.CreateFile("reports.xml");

            XmlSerializer ser = new XmlSerializer(typeof(List<Report>));

            ser.Serialize(file, reports);

            file.Close();
            file.Dispose();

        }

        public void load()
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();

            if (store.FileExists("reports.xml"))
            {
                IsolatedStorageFileStream file = store.OpenFile("reports.xml", FileMode.Open);
                XmlSerializer xml = new XmlSerializer(typeof(List<Report>));

                reports = xml.Deserialize(file) as List<Report>;

                file.Close();
                file.Dispose();
            }

        }

        private void search_TextChanged(object sender, TextChangedEventArgs e)
        {
            foreach (TextBlock item in ListPanel.Items) {

                if (!item.Text.Contains(search.Text))
                {
                    item.Visibility = Visibility.Collapsed;
                }
                else
                {
                    item.Visibility = Visibility.Visible;
                }
            }
                
               
        }

    }
}